<?php
    include __DIR__ . '/autoload.php';
    
    go(function (){
        
        $financeType = [1 => 'wx_charge', 2 => 'online_card_charge'];
        
        $today = strtotime(date('Y-m-d 00:00:00', time()));
        $yesterday = $today - 3600 * 24;
        
        $mysql = new lib\Mysql();
        
        $agentIncomeLogs = $mysql->query("select sum(`amount`) as amount, `agent_id` from `agent_finance_log` where add_time >= {$yesterday} and add_time < {$today} group by `agent_id`");
        
        $platformIncome = 0; //平台总收入
        
        
        if(!empty($agentIncomeLogs)) {
            $agentLogsCount = count($agentIncomeLogs); //共有多少个代理商
            
            $totalRows = $mysql->query("select `amount`, `agent_id`, `type` from `agent_finance_log` where add_time >= {$yesterday} and add_time < {$today}");
            
            foreach ($agentIncomeLogs as &$agentLog) {
                $agentId = $agentLog['agent_id'];
                foreach ($totalRows as $k => $row) {
                    if($agentId == $row['agent_id']) {
                        $type = $financeType[$row['type']];
                        if(!isset($agentLog[$type])) {
                            $agentLog[$type] = 0;
                        }
                        
                        $agentLog[$type] += $row['amount'];
                        
                        unset($totalRows[$k]);
                    }
                }
            }
            
            $offset = ceil($agentLogsCount / 100);
            
            for ($i = 0; $i < $agentLogsCount; $i ++) {
                $logs = [];
                for ($j = 0; $j < 100; $j ++) {
                    $log = array_shift($agentIncomeLogs);
                    if(empty($log)) {
                        break;
                    }else{
                        $value = ['amount' => $log['amount'], 'agent_id' => $log['agent_id'], 'day' => $yesterday];
                        foreach ($financeType as $type) {
                            $value[$type] = isset($log[$type]) ? $log[$type] : 0;
                        }
                        array_push($logs, $value);
                    }
                }
                
                if(!empty($logs)) {
                    $mysql->addAll('agent_day_income', $logs);
                }
            }
        }
        
        
        $storeIncomeLogs = $mysql->query("select sum(`amount`) as amount, `store_id` from `store_finance_log` where add_time >= {$yesterday} and add_time < {$today} group by `store_id`");
        
        
        
        if(!empty($storeIncomeLogs)) {
            $storeLogsCount = count($storeIncomeLogs);
            
            $totalRows = $mysql->query("select `amount`, `store_id`, `type` from `store_finance_log` where add_time >= {$yesterday} and add_time < {$today}");
            
            foreach ($storeIncomeLogs as &$storeLog) {
                $storeId = $storeLog['store_id'];
                foreach ($totalRows as $k => $row) {
                    if($storeId == $row['store_id']) {
                        $type = $financeType[$row['type']];
                        if(!isset($storeLog[$type])) {
                            $storeLog[$type] = 0;
                        }
                        
                        $storeLog[$type] += $row['amount'];
                        
                        unset($totalRows[$k]);
                    }
                }
            }
            
            $offset = ceil($storeLogsCount / 100);
            
            for ($i = 0; $i < $storeLogsCount; $i ++) {
                $logs = [];
                for ($j = 0; $j < 100; $j ++) {
                    $log = array_shift($storeLogsCount);
                    if(empty($log)) {
                        break;
                    }else{
                        $value = ['amount' => $log['amount'], 'store_id' => $log['store_id'], 'day' => $yesterday];
                        foreach ($financeType as $type) {
                            $value[$type] = isset($log[$type]) ? $log[$type] : 0;
                        }
                        array_push($logs, $value);
                    }
                }
                
                if(!empty($logs)) {
                    $mysql->addAll('store_day_income', $logs);
                }
            }
        }
        
        $platformDadyData = ['amount' => 0, 'day' => $yesterday];
        $storeIncomeLogs = $mysql->query("select `amount`, `type` from `platform_finance_log` where add_time >= {$yesterday} and add_time < {$today}");
        if(!empty($storeIncomeLogs)) {
            foreach ($storeIncomeLogs as $log) {
                $platformDadyData['amount'] += $log['amount'];
                $type = $financeType[$log['type']];
                if(!isset($platformDadyData[$type])) {
                    $platformDadyData[$type] = 0;
                }
                
                $platformDadyData[$type] += $log['amount'];
            }
            
            $mysql->add('platform_day_income', $platformDadyData);
        }
        
        $mysql->close();
        
    });
    
    
    
    
    
    
    
    
    